from flask_login import LoginManager, AnonymousUserMixin
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect
from flask_migrate import Migrate
from flask_apscheduler import APScheduler

db = SQLAlchemy()
login_manager = LoginManager()
csrf = CSRFProtect()
migrate = Migrate()
scheduler = APScheduler()


@login_manager.user_loader
def load_user(user_id):
    from app.models import User
    user = User.query.get(int(user_id))
    return user


class Guest(AnonymousUserMixin):

    def can(self, permission_name):
        return False

    @property
    def is_admin(self):
        return False

    @staticmethod
    def is_subscribing(crawler):
        return None


login_manager.anonymous_user = Guest
login_manager.login_view = 'auth.login'
login_manager.login_message_category = 'warning'
login_manager.login_message = '这个页面要登陆才能用'
